package com.nchu.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nchu.VO.UserVO;
import com.nchu.common.util.PageResult;
import com.nchu.entity.User;
import com.nchu.mapper.UserMapper;
import com.nchu.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yaojie
 * @since 2025-01-09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public PageResult<User> findByPage(Integer page, Integer pageSize) {
        //调用mybatisPlus的方法完成分页
        Page<User> pg = this.page(new Page<>(page, pageSize));
        // 返回分页的结果
        return new PageResult<>(pg.getRecords(),pg.getTotal());
    }

    @Override
    public PageResult<User> search(Integer page, Integer pageSize, UserVO userVO) {
        // 进行分页带条件查询
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //按照用户名模糊查找用户
        wrapper.like(StrUtil.isNotBlank(userVO.getUsername()), "username", userVO.getUsername());
        //按照用户地址模糊查找用户
        wrapper.like(StrUtil.isNotBlank(userVO.getAddress()), "address", userVO.getAddress());
        //按照用户电话模糊查找用户
        wrapper.like(StrUtil.isNotBlank(userVO.getPhone()),"phone",userVO.getPhone());
        Page<User> pg = this.page(new Page<>(page, pageSize), wrapper);
        return new PageResult<>(pg.getRecords(),pg.getTotal());
    }

    @Override
    public boolean addUser(User user) {
        return this.save(user);
    }
}
